package com.partners.controller.Page;

import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.constant.PermissionEnum;
import com.partners.service.TaskService;
import com.partners.service.UserService;
import com.partners.utils.CodeUtils;
import com.partners.utils.SessionManager;
import com.partners.utils.SharePageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author liangyongle
 * @date 2019-01-05
 */
@Controller
@RequestMapping("taskPage")
@Slf4j
public class TaskPageController {

    @Resource
    private SessionManager sessionManager;

    @Resource
    private UserService userService;

    @Resource
    private TaskService taskService;


    /**
     * 任务中心
     * @param
     * @return List
     *
     */
    @GetMapping("task_center")
    @Auths
    public ModelAndView taskCenter(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        Integer code = PermissionEnum.TASK_LIST.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/task_center";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        return mav;
    }

    /**
     * 编辑任务
     * @param
     * @return List
     *
     */
    @GetMapping("task_edit")
    @Auths
    public ModelAndView taskEdit(@RequestParam(value="id", required=false) Integer id,HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        Integer code = PermissionEnum.TASK_EDIT.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/task_edit";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        mav.addObject("uid",uid);
        mav.addObject("id",id);
        return mav;
    }


    /**
     * 获取员工的任务详情
     */
    @GetMapping("taskinfo")
    @Auths
    public ModelAndView taskInfo(HttpSession session,@RequestParam(value="id", required=false) Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        String openId = jsonObject.getString("openid");
        id=id!=null?id:5;
        //判断uid是否存在，不存在就增加浏览量
        if(uid == 0){ taskService.addReadTask(openId,id); }
        String url = "http://lyl.natapp1.cc/taskPage/taskinfo?id=5";
        ModelAndView mav = SharePageUtil.getShareSign(url, "show/task_info");
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 分享页面
     * @param
     * @return List
     */
    @GetMapping("share")
    public ModelAndView share(HttpServletRequest reques){
        String url = reques.getRequestURL().toString();
        ModelAndView mav = taskService.getShareView(url);
        return mav;
    }

}
